﻿using System.Collections.Generic;
using System.Dynamic;

namespace Isha.Web.Mvc
{
    public class IshaModel : DynamicObject
    {
        Dictionary<string, dynamic> _dynamicmodel;

        public IshaModel()
        {
            _dynamicmodel = new Dictionary<string, dynamic>();
        }

        public void Add(string Name,object model)
        {
            _dynamicmodel.Add(Name, model);
        }

        public T Get<T>()
        {
            if (!_dynamicmodel.ContainsKey(typeof(T).Name))
                return default(T);
            else
                return (T)_dynamicmodel[typeof(T).Name];
        }


        public dynamic Get(string Name)
        {
            if (!_dynamicmodel.ContainsKey(Name))
                return null;
            else
                return _dynamicmodel[Name];
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            if (!_dynamicmodel.ContainsKey(binder.Name))
                result = null;
            else
                result = _dynamicmodel[binder.Name];       
            return true;
        }
    }
}
